{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Numpy数组属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n",
      "[[ 1  2  3]\n",
      " [ 4  5  6]\n",
      " [ 7  8  9]\n",
      " [10 11 12]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "vector = np.array([1,2,3])\n",
    "matrix = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])\n",
    "#print(vector.shape)\n",
    "#print(vector.ndim)\n",
    "#print(matrix.ndim)\n",
    "print(vector)\n",
    "print(matrix)\n",
    "# matrix = np.array([1,2,3],[4,5,6],[7,8,9])这种表示方法是错误的\n",
    "#print(vector.T)\n",
    "#print(matrix.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "print(vector.ndim)\n",
    "print(matrix.ndim)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 3)\n",
      "(3,)\n",
      "(4, 3)\n",
      "[4 5 6]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vector2 = np.array([[1,2,3]])\n",
    "print(vector2.shape)\n",
    "print(vector.shape)\n",
    "print(matrix.shape)\n",
    "print(matrix[1])\n",
    "matrix[1][2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n",
      "[[1]\n",
      " [2]\n",
      " [3]]\n",
      "[[ 1  4  7 10]\n",
      " [ 2  5  8 11]\n",
      " [ 3  6  9 12]]\n"
     ]
    }
   ],
   "source": [
    "#转置矩阵\n",
    "print(vector.T)\n",
    "print(vector2.T)\n",
    "print(matrix.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<numpy.flatiter object at 0x000001DF394DE0C0>\n",
      "<numpy.flatiter object at 0x000001DF394DE0C0>\n",
      "[1, 2, 3]\n",
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]\n"
     ]
    }
   ],
   "source": [
    "#  flat返回一维迭代器\n",
    "print(vector.flat)\n",
    "print(matrix.flat)\n",
    "print([x for x in vector.flat])\n",
    "print([x for x in matrix.flat])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "12\n"
     ]
    }
   ],
   "source": [
    "# size属性 计算元素个数\n",
    "print(vector.size)\n",
    "print(matrix.size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "4\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "# itemsize  计算数组元素的位数\n",
    "print(vector.itemsize)\n",
    "print(matrix.itemsize)\n",
    "a = np.array([1,2,3],dtype = float)\n",
    "print(a.itemsize)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n",
      "48\n"
     ]
    }
   ],
   "source": [
    "# nbytes 属性 （所有元素的字节大小，nbytes = size *  itemsize ）\n",
    "print(vector.nbytes)\n",
    "print(matrix.nbytes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4,)\n",
      "(12, 4)\n"
     ]
    }
   ],
   "source": [
    "# strides 属性（步长：按照一维数组的获取的元素的方式）\n",
    "print ( vector.strides)\n",
    "print ( matrix.strides)   # 二维按照一维形式获取元素位12个位，每个元素中的单个元素为4个位"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int32\n",
      "float64\n",
      "int32\n"
     ]
    }
   ],
   "source": [
    "# dtype输出数组元素的类型\n",
    "print(vector.dtype)\n",
    "print(a.dtype)\n",
    "print(matrix.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<numpy.core._internal._ctypes object at 0x000001DF394640C8>\n",
      "<numpy.core._internal._ctypes object at 0x000001DF394646C8>\n",
      "<memory at 0x000001DF38FD87C8>\n",
      "<memory at 0x000001DF394919E8>\n",
      "  C_CONTIGUOUS : True\n",
      "  F_CONTIGUOUS : True\n",
      "  OWNDATA : True\n",
      "  WRITEABLE : True\n",
      "  ALIGNED : True\n",
      "  WRITEBACKIFCOPY : False\n",
      "  UPDATEIFCOPY : False\n",
      "  C_CONTIGUOUS : True\n",
      "  F_CONTIGUOUS : False\n",
      "  OWNDATA : True\n",
      "  WRITEABLE : True\n",
      "  ALIGNED : True\n",
      "  WRITEBACKIFCOPY : False\n",
      "  UPDATEIFCOPY : False\n",
      "None\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "# 其他属性\n",
    "print ( vector.ctypes)     #输出数组对象\n",
    "print ( matrix.ctypes)\n",
    "print ( vector.data)       #数据内存地址。\n",
    "print ( matrix.data)\n",
    "print ( vector.flags)       #内存分布信息\n",
    "print ( matrix.flags)\n",
    "print ( vector.base)       #来自其他对象的Base对象\n",
    "print ( matrix.base)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Numpy数组的索引和切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n",
      "[[ 1  2  3]\n",
      " [ 4  5  6]\n",
      " [ 7  8  9]\n",
      " [10 11 12]]\n"
     ]
    }
   ],
   "source": [
    "print(vector)\n",
    "print(matrix)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "[1 2]\n",
      "[1 2]\n",
      "[3 2]\n",
      "[   1 2000    3]\n"
     ]
    }
   ],
   "source": [
    "# 一维数组的索引和切片\n",
    "print(vector[1])\n",
    "print(vector[0:2])\n",
    "print(vector[:2])\n",
    "# 反向索引切片\n",
    "print(vector[-1:-3:-1])\n",
    "#设置值\n",
    "vector[1]=2000\n",
    "print(vector)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3]\n",
      " [ 4  5  6]\n",
      " [ 7  8  9]\n",
      " [10 11 12]]\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "# 二维数组的索引和切片\n",
    "print(matrix)\n",
    "print(matrix[1][2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 4  5  6]\n",
      " [ 4  5  6]\n",
      " [ 7  8  9]\n",
      " [10 11 12]]\n",
      "[[ 4  5  6]\n",
      " [ 4  0  6]\n",
      " [ 7  8  9]\n",
      " [10 11 12]]\n",
      "[[ 4  5  6]\n",
      " [ 4  1  6]\n",
      " [ 7  8  9]\n",
      " [10 11 12]]\n"
     ]
    }
   ],
   "source": [
    "# 索引设置值\n",
    "matrix[0] = [4,5,6]\n",
    "# matrix[0] = 1 这种形式是错误的值设置必须维数要与原先维数一致\n",
    "print(matrix)\n",
    "matrix[1][1] = 0 #必须与位置对应形式一致\n",
    "print(matrix)\n",
    "matrix[1][1] = 1.1 #数组中的元素其数据类型必须一致，浮点型自动转换成整型\n",
    "print(matrix)\n",
    "#matrix[1][1] = [1,2,3] 此种方式错误\n",
    "#print(matrix)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[1 2 3]\n",
      "  [1 2 3]\n",
      "  [1 2 3]]\n",
      "\n",
      " [[4 5 6]\n",
      "  [4 5 6]\n",
      "  [4 5 6]]]\n",
      "3\n",
      "[[list([1, 2]) list([1, 2, 3]) list([1, 2, 3])]\n",
      " [list([4, 5, 6]) list([4, 5, 6]) list([4, 5, 6])]]\n",
      "2\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\n数组的每一个维度其元素个数必须一致\\n如若不一致则进行降低维度打包处理使得维度变得一致\\n如matrix2中有[1,2]与其同级别维度元素个数不一致则将其看作一整体\\n即元素变成list形式处理\\n'"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matrix2 = np.array([[[1,2,3],[1,2,3],[1,2,3]],[[4,5,6],[4,5,6],[4,5,6]]])\n",
    "print(matrix2)\n",
    "print(matrix2.ndim)\n",
    "matrix2 = np.array([[[1,2],[1,2,3],[1,2,3]],[[4,5,6],[4,5,6],[4,5,6]]])\n",
    "print(matrix2)\n",
    "print(matrix2.ndim)\n",
    "'''\n",
    "数组的每一个维度其元素个数必须一致\n",
    "如若不一致则进行降低维度打包处理使得维度变得一致\n",
    "如matrix2中有[1,2]与其同级别维度元素个数不一致则将其看作一整体\n",
    "即元素变成list形式处理\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 4  5  6]\n",
      " [ 4  1  2]\n",
      " [ 7  2  1]\n",
      " [10 11 12]]\n",
      "1\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "# ndarray与list索引特殊之处可以使用元组组合索引下标依次选取\n",
    "print(matrix)\n",
    "# 下面两种方式一致\n",
    "print(matrix[1,1])\n",
    "print(matrix[1][1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 6]\n",
      " [8 9]]\n",
      "[[12 11]\n",
      " [ 9  8]]\n"
     ]
    }
   ],
   "source": [
    "# 切片\n",
    "# 子矩阵的选取\n",
    "print(matrix[1:3,1:3])\n",
    "# 这种形式是错误的print(matrix[1:2][1:2])\n",
    "# 反向索引选取子矩阵\n",
    "print(matrix[-1:-3:-1,-1:-3:-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 4  5  6]\n",
      " [ 4  1  6]\n",
      " [ 7  8  9]\n",
      " [10 11 12]]\n",
      "[[ 4  5  6]\n",
      " [ 4  1  2]\n",
      " [ 7  2  1]\n",
      " [10 11 12]]\n"
     ]
    }
   ],
   "source": [
    "# set values\n",
    "print(matrix)\n",
    "matrix[1:3,1:3] = [[1,2],[2,1]]\n",
    "print(matrix)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ndarry的特殊索引和切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[   1 2000    3]\n",
      "[   3 2000    1]\n",
      "[3000 2000 1000]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "print(vector)\n",
    "print(vector[[2,1,0]]) # 依次取数组下标为2,1,0的元素构成新的数组\n",
    "vector[[2,0]] = [1000,3000]\n",
    "print(vector)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4]\n",
      " [ 5  6  7  8]\n",
      " [ 9 10 11 12]\n",
      " [13 14 15 16]]\n",
      "[ 2 12]\n",
      "[ 2 12]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\nprint(matrix[[0,1],[1,2],[0,1]])这是错误的,因为我想将\\nmatrix中0,1,0、1,2,1位置的元素取出来做成一个新的数组\\n但是我定义的matrix是二维的不是三维的\\n'"
      ]
     },
     "execution_count": 177,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 二维数组特殊的索引\n",
    "matrix = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])\n",
    "print(matrix)\n",
    "# 返回0,1、2,3位置的元素构成新数组下面两种方式一致\n",
    "print(matrix[[0,2],[1,3]])\n",
    "print(matrix[[0,2],[1,3],])\n",
    "'''\n",
    "print(matrix[[0,1],[1,2],[0,1]])这是错误的,因为我想将\n",
    "matrix中0,1,0、1,2,1位置的元素取出来做成一个新的数组\n",
    "但是我定义的matrix是二维的不是三维的\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2 12]\n",
      " [ 6 16]]\n",
      "[[ 2 12]\n",
      " [ 6 16]]\n"
     ]
    }
   ],
   "source": [
    "print(matrix[[[0,2],[1,3]],[1,3]]) #相当于把[0,2],[1,3]与[1,3]做笛卡尔积\n",
    "#上面的式子等价与下面式子\n",
    "print(matrix[[[0,2],[1,3]],[[1,3],[1,3]]])\n",
    "print(matrix)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 178,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4]\n",
      " [ 5  6  7  8]\n",
      " [ 9 10 11 12]\n",
      " [13 14 15 16]]\n",
      "[2 3]\n",
      "[ 1  6 11 16]\n"
     ]
    }
   ],
   "source": [
    "# 取第一行的第二列和三列元素\n",
    "print(matrix)\n",
    "print(matrix[0][1:3])#这个一般用于取子矩阵或者是取行列交叉的元素\n",
    "#取矩阵的对角元素\n",
    "print(matrix[[0,1,2,3],[0,1,2,3]]) #这个一般用于取不同行不同列的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 186,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4]\n",
      " [ 5  6  7  8]\n",
      " [ 9 10 11 12]\n",
      " [13 14 15 16]]\n",
      "[[[ 1  2  3  4]\n",
      "  [ 5  6  7  8]]\n",
      "\n",
      " [[ 5  6  7  8]\n",
      "  [ 9 10 11 12]]]\n",
      "[[ 3  8]\n",
      " [ 7 12]]\n"
     ]
    }
   ],
   "source": [
    "print(matrix)\n",
    "print ( matrix [ [ [ 0, 1] , [1, 2 ] ] ,] )    #第0,1、1,2行构成的矩阵\n",
    "print ( matrix [ [ [ 0, 1] , [1, 2 ] ] , [ [2, 3 ] ] ] )   # 取0，11，2行，分别再[取0行2列 ，1行3列]，[1行2列，2行3列]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 198,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  0  0  0]\n",
      " [ 0  0  0  0]\n",
      " [ 9 10 11 12]\n",
      " [13 14 15 16]]\n",
      "[[False False False False]\n",
      " [False False False False]\n",
      " [ True  True  True  True]\n",
      " [ True  True  True  True]]\n",
      "[[ 0  0  0  0]\n",
      " [ 0  0  0  0]\n",
      " [ 9 10 11 12]\n",
      " [13 14 15 16]]\n",
      "[[ 0  0  0  0]\n",
      " [ 0  0  0  0]\n",
      " [ 9 10 11 12]\n",
      " [13 14 15 16]]\n"
     ]
    }
   ],
   "source": [
    "# 返回布尔值表这个对于我们数字图像处理灰度值的时候蛮有用\n",
    "print(matrix)\n",
    "print(matrix > 8)\n",
    "print(matrix)\n",
    "# 根据布尔值修改元素值\n",
    "matrix[matrix<9] = 0\n",
    "print(matrix)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
